package com.bnzj.cloud.business.core.service;

import com.bnzj.cloud.business.core.persistence.entity.SysRoleUser;
import com.bnzj.cloud.business.core.persistence.entity.SysRoleUserDept;
import com.bnzj.cloud.business.core.persistence.entity.SysUser;
import com.bnzj.cloud.business.core.persistence.repository.SysRoleUserDeptRepository;
import com.bnzj.cloud.business.core.persistence.repository.SysRoleUserRepository;
import com.bnzj.cloud.business.core.persistence.repository.SysUserRepository;
import com.bnzj.cloud.business.core.vo.RoleUserDeptVo;
import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SysRoleUserService extends BaseService<SysRoleUserRepository, SysRoleUser, Long> {


    @Autowired
    private SysRoleUserDeptRepository sysRoleUserDeptRepository;


    public List<SysRoleUser> findRoleUserByuserId(Long id) {

        return repository.findSysRoleUserByUserId(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean portionRole(List<SysRoleUser> roleUsers, Long userId) {
        boolean flag = false;
        try {
            repository.deleteByUserId(userId);
            repository.saveAll(roleUsers);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    @Transactional(rollbackFor = Exception.class)
    public void portionUser(List<SysRoleUser> roleUsers, Long roleId) {
        repository.deleteByRoleId(roleId);
        repository.saveAll(roleUsers);
    }

    /**
     * @param roleId
     * @return java.util.List<java.lang.Long>
     * @depict 根据角色id获取角色下所有用户id
     * @auth Barry
     * @createDate 2020/2/22
     * @updateAuthOrOther
     */
    public List<Long> getUserIdsByRoleId(Long roleId) {
        return repository.findUserIdsByRoleId(roleId);
    }


    public List<Long> getRoleIdsByUserId(Long userId) {
        return repository.findRoleIdsByUserId(userId);
    }

    @Autowired
    private SysUserRepository sysUserRepository;

    public List<SysUser> getUsersByRoleId(Long roleId) {
        return sysUserRepository.findUsersByRoleId(roleId);
    }

    public List<SysUser> getBaseUsersByRoleId(Long roleId) {
        return sysUserRepository.findBaseUsersByRoleId(roleId);
    }
    
    public void deleteByUserId(Long id) {
        repository.deleteByUserId(id);
    }

    public void deleteByRoleId(Long id) {
        repository.deleteByRoleId(id);
    }

    /**
     * 根据角色code查询拥有该角色的用户的账号
     *
     * @param roleCode
     * @return
     */
    public String findAccountsByRoleCode(String roleCode) {
        return repository.findAccountsByRoleCode(roleCode);
    }


    @Transactional(rollbackFor = Exception.class)
    public void portionRoleDept(List<RoleUserDeptVo> roleUserDeptVos, UserInfo userInfo) {
        Set<SysRoleUserDept> sysRoleUserDepts = new HashSet<>();
        roleUserDeptVos.forEach(a -> {
            //用户角色关联
            SysRoleUser sysRoleUser = new SysRoleUser();
            sysRoleUser.setRoleId(a.getRoleId());
            sysRoleUser.setUserId(a.getUserId());
            EntityUtils.saveBefore(sysRoleUser, userInfo.getAccount(), userInfo.getName());
            SysRoleUser sysRoleUserDb = this.repository.save(sysRoleUser);
            List<Long> deptIds = a.getDeptIds();
            //用户部门关联
            deptIds.stream().forEach(b -> {
                SysRoleUserDept sysRoleUserDept = new SysRoleUserDept();
                sysRoleUserDept.setRoleUserId(sysRoleUserDb.getId());
                sysRoleUserDept.setDeptId(b);
                EntityUtils.saveBefore(sysRoleUserDept, userInfo.getAccount(), userInfo.getName());
                sysRoleUserDepts.add(sysRoleUserDept);
            });
        });
        sysRoleUserDeptRepository.saveAll(sysRoleUserDepts);

    }
}
